package myserver.core;
import java.io.*;

import myserver.WebServer;
/**
 * 相应输出工具类
 * @author Leeyn
 *
 */
public class Response {
    private OutputStream output;

    public Response(OutputStream output) {
        this.output = output;
    }

    /**
     * 输出文本信息
     * @param text
     * @throws IOException
     */
    public void writeText(String text) {
        FileInputStream fis = null;
        try {
            output.write("HTTP/1.1 200 OK\n".getBytes());
            output.write("Content-Type: text/html; charset=UTF-8\n\n".getBytes());
            output.write(text.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * 输出json信息
     * @param json
     * @throws IOException
     */
    public void writeJson(String json) {
        FileInputStream fis = null;
        try {
            output.write("HTTP/1.1 200 OK\n".getBytes());
            output.write("Content-Type: application/json; charset=UTF-8\n\n".getBytes());
            output.write(json.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private static final int BUFFER_SIZE = 1024;
    public void writeStaticResource(String uri){
    	
        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        try {
            //将web文件写入到OutputStream字节流中
            File file = new File(WebServer.WEB_ROOT, uri);
            if (file.exists()) {
            	
            	//TODO 自行扩展  更多静态文件类型
            	if(uri.endsWith(".rar")) {
            		output.write("HTTP/1.1 200 OK\n".getBytes());
                    output.write("Content-Type: application/octet-stream; charset=UTF-8\n\n".getBytes());
            	}
            	else {
            		output.write("HTTP/1.1 200 OK\n".getBytes());
                    output.write("Content-Type: text/html; charset=UTF-8\n\n".getBytes());
            	}            	               
                
                fis = new FileInputStream(file);
                int ch = fis.read(bytes, 0, BUFFER_SIZE);
                while (ch != -1) {
                    output.write(bytes, 0, ch);
                    ch = fis.read(bytes, 0, BUFFER_SIZE);
                }
            } else {
                // file not found
                String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n"
                        + "Content-Length: 23\r\n" + "\r\n" + "<h1>File Not Found</h1>";
                output.write(errorMessage.getBytes());
            }
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
        	if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //TODO 自行扩展 更多响应方案

    public  void writeBytesToFile(InputStream inputStream,int index){
        String filepath=WebServer.WEB_ROOT + "/upload.rar";
        BufferedOutputStream out = null;
        int a = -1;
        try{
            out = new BufferedOutputStream(new FileOutputStream(filepath));
            inputStream.skip(index);
            byte[] buffer=new byte[1024];
            while((a = inputStream.read(buffer)) != -1){
                out.write(buffer,0,a);
            }
            output.write("HTTP/1.1 200 OK\n".getBytes());
            output.write("Content-Type: text/plain; charset=UTF-8\n\n".getBytes());
            output.write(filepath.getBytes());
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                if(out!=null){
                    out.flush();
                    out.close();
                }
                if(inputStream!=null){
                    inputStream.close();
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }

        }


    }

}